const app = getApp()
Page({
  data: {
    loadingHidden: false,//代表出现加载
    editimage: {
      sort: 0,
      img_src: ''
    },
    imgcount:0,//当前总共有几张图片
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "编辑图片"
    })
    var obj={};
    obj.detail = options.detail;
    obj.sort = options.sort;
    obj.img_src = options.img_src;
    var count = options.count;
    this.setData({ editimage: obj });
    this.setData({ imgcount: count });
  },

  //调取相机 如果是相册上传。只能限制一张图片
  handlePhoto: function (options) {
    var that = this;
    var editimage = that.data.editimage;
    wx.chooseImage({
      count: 1, 
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.setData({ loadingHidden: false });
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var serviceimage = [];
        if (tempFilePaths.length > 0) {
          for (var i = 0; i < tempFilePaths.length; i++) {
            var num = i;
            wx.uploadFile({
              url: app.globalData.baseUrl + 'wxuploadimage',
              filePath: tempFilePaths[i],
              name: 'file',
              header: {
                "Content-Type": "multipart/form-data"
              },
              success: function (res) {
                that.setData({ loadingHidden: true });
                var obj = JSON.parse(res.data);
                if (obj.code == 200) {
                  var uploadsrc = obj.data;//图片服务器路径
                  that.setData({ 'editimage.img_src': uploadsrc });
                  let pages = getCurrentPages();
                  let currPage = null; //当前页面
                  let prevPage = null; //上一个页面
                  if (pages.length >= 2) {
                    currPage = pages[pages.length - 1]; //当前页面
                    prevPage = pages[pages.length - 2]; //上一个页面
                    if (prevPage) {
                      prevPage.setData({
                        editimage: editimage
                      });
                    }
                  }
                  wx.navigateBack({
                    delta: 1
                  })
                }
              }
            })
          }
        }
      }
    })
  },
  //删除相册
  delphotodata: function (event) {
    var that = this;
    var imgcount = that.data.imgcount;
    var editimage = that.data.editimage;
    if (imgcount < 2) {
      wx.showToast({
        title: '至少保留一张',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    wx.showModal({
      title: '确定删除此图片?',
      content: '不会删除系统相册中的图片',
      success: function (res) {
        if (res.confirm) {
          let pages = getCurrentPages();
          let currPage = null; //当前页面
          let prevPage = null; //上一个页面
          if (pages.length >= 2) {
            currPage = pages[pages.length - 1]; //当前页面
            prevPage = pages[pages.length - 2]; //上一个页面
            if (prevPage) {
              editimage.img_src="";
              console.log(editimage);
              prevPage.setData({
                delimagedetail: editimage
              });
            }
          }
          wx.navigateBack({
            delta: 1
          })

        } else {
        }
      }
    })
  },
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})